﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using Game.Editor;
using Game.Map;
using Game.Toolkit;
using Game.Editor.Toolbox;
using Game.Level;
using Game.Terrain;

namespace GameEditor
{
    public partial class MainPage : UserControl
    {
        readonly CampaignTreeControl _CampaignTreeControl;

        static MainPage()
        {
            StandardToolkit.RegisterResources();
        }

        public MainPage()
        {
            _CampaignTreeControl = new CampaignTreeControl();

            InitializeComponent();
            Init();
        }

        void Init()
        {
            SelectionController controller = new SelectionController();
            controller.Start();

            this.grid1.Children.Add(_CampaignTreeControl);
            _CampaignTreeControl.Height = double.NaN;
            _CampaignTreeControl.Width = double.NaN;
            _CampaignTreeControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _CampaignTreeControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

            _CampaignTreeControl.GameLevelSelected += this.OnLevelSelected;
            _CampaignTreeControl.GameMilestoneSelected += this.OnMilestoneSelected;
            _CampaignTreeControl.GameClose += this.OnLevelClosed;
        }

        void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            MapEditorControl c = this.tabControl1.SelectedContent as MapEditorControl;
            if (c != null)
            {
                c.SurfaceControl.GameScale = (e.NewValue / 100d);
            }
        }

        void OnLevelClosed(object sender, EventArgs e)
        {
            this.tabControl1.Items.Clear();
        }

        void OnLevelSelected(object sender, LevelEventArgs e)
        {
            DisplayMap(e.GameLevel);
        }

        void OnMilestoneSelected(object sender, LevelMilestoneEventArgs e)
        {
            DisplayMilestone(e.GameLevel, e.GameMilestone);
        }

        void DisplayMap(GameLevel level)
        {
            IGameMap map = level.GameMap;
            _CampaignTreeControl.Cursor = System.Windows.Input.Cursors.Wait;

            bool found = false;
            foreach (TabItem tabItem in this.tabControl1.Items)
            {
                MapEditorControl c = tabItem.Content as MapEditorControl;
                if (c != null && c.SurfaceControl.GameMapView.GameMap == map)
                {
                    tabItem.IsSelected = true;
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                MapEditorControl c = new MapEditorControl(map);
                TabItem tabItem = new TabItem() { Content = c };
                tabItem.Header = "Level " + GameLevels.GetIndex(level);
                tabItem.Tag = level;
                this.tabControl1.Items.Add(tabItem);
                tabItem.IsSelected = true;
            }

            _CampaignTreeControl.Cursor = System.Windows.Input.Cursors.Arrow;
        }

        void DisplayMilestone(GameLevel level, GameMilestone milestone)
        {
            bool found = false;
            foreach (TabItem tabItem in this.tabControl1.Items)
            {
                MilestoneEditorControl c = tabItem.Content as MilestoneEditorControl;
                if (c != null && c.Milestone == milestone)
                {
                    tabItem.IsSelected = true;
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                MilestoneEditorControl c = new MilestoneEditorControl(milestone);
                TabItem tabItem = new TabItem() { Content = c };
                tabItem.Header = "Level " + GameLevels.GetIndex(level) + " (" + milestone.EventType + ")";
                tabItem.Tag = milestone;
                this.tabControl1.Items.Add(tabItem);
                tabItem.IsSelected = true;
            }
        }

    }
}
